package com.huoyun.userAdmin.action;


import java.util.List;
import java.util.Set;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;

import com.huoyun.base.action.BaseAction;
import com.huoyun.base.bean.Page;
import com.huoyun.car.service.CarService;
import com.huoyun.cargo.service.CargoService;
import com.huoyun.cargo.service.evt.GetCargosByUserIdEvt;
import com.huoyun.common.bean.BidInfoItem;
import com.huoyun.common.model.InviteCarModel;
import com.huoyun.common.service.BidInfoService;
import com.huoyun.common.service.InviteCarService;
import com.huoyun.common.service.evt.GetBidInfoItemEvt;
import com.huoyun.common.service.evt.QueryInviteCarModelEvt;
import com.huoyun.company.bean.CompanyInfo;
import com.huoyun.company.bean.CompanyPerson;
import com.huoyun.company.service.CompanyService;
import com.huoyun.order.service.OrderService;
import com.huoyun.order.service.RateService;
import com.huoyun.order.service.evt.QueryOrderListEvt;
import com.huoyun.user.bean.Role;
import com.huoyun.user.bean.User;
import com.huoyun.user.service.UserService;
import com.huoyun.userAdmin.bean.UserDetail;
import com.huoyun.userAdmin.service.UserDetailService;

public class IndexAction extends BaseAction{
	private UserService userService;
	private UserDetailService userDetailService;
	private CargoService cargoService;
	private CarService carService;
	private CompanyService companyService;
	private RateService rateService;
	private OrderService orderService;
	/**
	 * 管理车辆邀请的service
	 */
	private InviteCarService inviteCarService;
	/**
	 * 竞标情况的service
	 */
	private BidInfoService bidInfoService;
	
	private String userInfoCompleteRate;
	private String companyCompleteRate;
	private CompanyInfo company;
	private CompanyPerson companyPerson;
	private User user;
	private UserDetail userDetail;
	private Page cargoPage;
	
	private String unPayOrderCount;
	private String unConfirmCount;
	private String unCommentCount;
	
	/**
	 * 车主主动应标的详细应标信息列表
	 */
	private Page myBiddingInfoList;
	/**
	 * 车主已经达成的应标详细信息列表
	 */
	private Page mySuccessBidInfoList;
	/**
	 * 车主收到的邀请的详细信息列表
	 */
	private Page myInviteCarModelList;
	
	
	private String roleType;
	
	public String index()
	{
		user=this.getLoginUser();
		long userId=user.getId();
		userDetail=userDetailService.getUserDetailByUserId(userId);
		company=companyService.getCompanyInfoByUserId(userId);
		if(company!=null){
			companyPerson=companyService.getCompanyPersonByCompanyId(company.getId());
		}
		
		Set<GrantedAuthority> authoritys = (Set<GrantedAuthority>) getLoginUser().getAuthorities();
		
		//查询待付款订单数量
		QueryOrderListEvt queryOrderListEvt1 = new QueryOrderListEvt();

		queryOrderListEvt1.setStatus("1");
		queryOrderListEvt1.setPageCount(PAGE_COUNT);
		queryOrderListEvt1.setPageNo(pageNo);
		
		//查询待确认订单数量
		
		QueryOrderListEvt queryOrderListEvt2 = new QueryOrderListEvt();

		queryOrderListEvt2.setStatus("2");
		queryOrderListEvt2.setPageCount(PAGE_COUNT);
		queryOrderListEvt2.setPageNo(pageNo);

	
		//查询待评价订单数量
		QueryOrderListEvt queryOrderListEvt3 = new QueryOrderListEvt();

		queryOrderListEvt3.setStatus("3");
		queryOrderListEvt3.setPageCount(PAGE_COUNT);
		queryOrderListEvt3.setPageNo(pageNo);
	
		if (authoritys.contains(new GrantedAuthorityImpl(Role.ROLE_USER))) {
			queryOrderListEvt1.setCargosOwnerId(userId);
			queryOrderListEvt2.setCargosOwnerId(userId);
			queryOrderListEvt3.setCargosOwnerId(userId);
			this.roleType=Role.ROLE_USER;
		} else if (authoritys.contains(new GrantedAuthorityImpl(Role.ROLE_DRIVER))) {
			queryOrderListEvt1.setCarOwnerId(userId);
			queryOrderListEvt2.setCarOwnerId(userId);
			queryOrderListEvt3.setCarOwnerId(userId);
			this.roleType=Role.ROLE_DRIVER;
		}
		unPayOrderCount=orderService.queryOrderCount(queryOrderListEvt1)+"";
		
		unConfirmCount=orderService.queryOrderCount(queryOrderListEvt2)+"";
		
		unCommentCount=orderService.queryOrderCount(queryOrderListEvt3)+"";
		
		double userComplete=(user.getCompleteRate()+userDetail.getCompleteRate()+0.0)/(user.getRateSum()+userDetail.getRateSum());
		userInfoCompleteRate=(Math.round(userComplete*100)/100.0)*100+"";
		
		if(company!=null){
			if(companyPerson!=null){
				double companyComplete=(company.getCompleteRate()+companyPerson.getCompleteRate()+0.0)/(company.getRateSum()+companyPerson.getRateSum());
				companyCompleteRate=(Math.round(companyComplete*100))*100+"";
			}else{
				double companyComplete=(company.getCompleteRate()+0.0)/(company.getRateSum());
				companyCompleteRate=(Math.round(companyComplete*100))*100+"";
			}
		}
		
		if(roleType.equalsIgnoreCase(Role.ROLE_USER)){
			GetCargosByUserIdEvt evt = new GetCargosByUserIdEvt();
			evt.setUserId(getLoginUser().getId());
		
			evt.setPageNo(pageNo);
			evt.setPageCount(PAGE_COUNT);
			
			cargoPage = cargoService.getCargosByUserId(evt);
		}else if(roleType.equalsIgnoreCase(Role.ROLE_DRIVER)){
			GetBidInfoItemEvt evt1=new GetBidInfoItemEvt();
			evt1.setPageCount(5);
			evt1.setUserId(userId);
			evt1.setPageNo(pageNo);
			evt1.setStatus(0);
			
			QueryInviteCarModelEvt evt2=new QueryInviteCarModelEvt();
			evt2.setPageCount(5);
			evt2.setPageNo(pageNo);
			evt2.setUserId(userId);
			
			
			GetBidInfoItemEvt evt3=new GetBidInfoItemEvt();
			evt3.setPageCount(5);
			evt3.setUserId(userId);
			evt3.setPageNo(pageNo);
			evt3.setStatus(2);
			this.myBiddingInfoList = bidInfoService.queryBidInfoItemByStatusForUser(evt1);
			
			this.myInviteCarModelList = inviteCarService.queryInviteCarModelListByUserId(evt2);
			
			this.mySuccessBidInfoList = bidInfoService.queryBidInfoItemByStatusForUser(evt3);
		}
		
		return SUCCESS;
	}
	
	public String accountIndex()
	{
		try {
			user=this.getLoginUser();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String messageIndex()
	{
		return SUCCESS;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserDetailService getUserDetailService() {
		return userDetailService;
	}

	public void setUserDetailService(UserDetailService userDetailService) {
		this.userDetailService = userDetailService;
	}

	public CargoService getCargoService() {
		return cargoService;
	}

	public void setCargoService(CargoService cargoService) {
		this.cargoService = cargoService;
	}

	public CarService getCarService() {
		return carService;
	}

	public void setCarService(CarService carService) {
		this.carService = carService;
	}

	public CompanyService getCompanyService() {
		return companyService;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

	public String getUserInfoCompleteRate() {
		return userInfoCompleteRate;
	}

	public void setUserInfoCompleteRate(String userInfoCompleteRate) {
		this.userInfoCompleteRate = userInfoCompleteRate;
	}

	public String getCompanyCompleteRate() {
		return companyCompleteRate;
	}

	public void setCompanyCompleteRate(String companyCompleteRate) {
		this.companyCompleteRate = companyCompleteRate;
	}

	public RateService getRateService() {
		return rateService;
	}

	public void setRateService(RateService rateService) {
		this.rateService = rateService;
	}
	
	public OrderService getOrderService() {
		return orderService;
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	public CompanyInfo getCompany() {
		return company;
	}

	public void setCompany(CompanyInfo company) {
		this.company = company;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserDetail getUserDetail() {
		return userDetail;
	}

	public void setUserDetail(UserDetail userDetail) {
		this.userDetail = userDetail;
	}

	public Page getCargoPage() {
		return cargoPage;
	}

	public void setCargoPage(Page cargoPage) {
		this.cargoPage = cargoPage;
	}

	public CompanyPerson getCompanyPerson() {
		return companyPerson;
	}

	public void setCompanyPerson(CompanyPerson companyPerson) {
		this.companyPerson = companyPerson;
	}

	public String getUnPayOrderCount() {
		return unPayOrderCount;
	}

	public void setUnPayOrderCount(String unPayOrderCount) {
		this.unPayOrderCount = unPayOrderCount;
	}

	public String getUnConfirmCount() {
		return unConfirmCount;
	}

	public void setUnConfirmCount(String unConfirmCount) {
		this.unConfirmCount = unConfirmCount;
	}

	public String getUnCommentCount() {
		return unCommentCount;
	}

	public void setUnCommentCount(String unCommentCount) {
		this.unCommentCount = unCommentCount;
	}

	public Page getMyBiddingInfoList() {
		return myBiddingInfoList;
	}

	public void setMyBiddingInfoList(Page myBiddingInfoList) {
		this.myBiddingInfoList = myBiddingInfoList;
	}

	public Page getMySuccessBidInfoList() {
		return mySuccessBidInfoList;
	}

	public void setMySuccessBidInfoList(Page mySuccessBidInfoList) {
		this.mySuccessBidInfoList = mySuccessBidInfoList;
	}

	public Page getMyInviteCarModelList() {
		return myInviteCarModelList;
	}

	public void setMyInviteCarModelList(Page myInviteCarModelList) {
		this.myInviteCarModelList = myInviteCarModelList;
	}

	public String getRoleType() {
		return roleType;
	}

	public void setRoleType(String roleType) {
		this.roleType = roleType;
	}

	public InviteCarService getInviteCarService() {
		return inviteCarService;
	}

	public void setInviteCarService(InviteCarService inviteCarService) {
		this.inviteCarService = inviteCarService;
	}

	public BidInfoService getBidInfoService() {
		return bidInfoService;
	}

	public void setBidInfoService(BidInfoService bidInfoService) {
		this.bidInfoService = bidInfoService;
	}
	
}
